<template>
    <div class="d2-dialog">
        <el-dialog
                :title=textMap[status].title
                :visible.sync="dialogVisible">
            <el-form ref="payChannelForm" :rules="rules" :model="payChannelForm" label-width="80px">
                <el-form-item label="" prop="id">
                    <el-input v-model="payChannelForm.id"></el-input>
                </el-form-item>
                <el-form-item label="" prop="provider_id">
                    <el-input v-model="payChannelForm.provider_id"></el-input>
                </el-form-item>
                <el-form-item label="" prop="name">
                    <el-input v-model="payChannelForm.name"></el-input>
                </el-form-item>
                <el-form-item label="图标名称" prop="icon">
                    <el-input v-model="payChannelForm.icon"></el-input>
                </el-form-item>
                <el-form-item label="" prop="bean_service_name">
                    <el-input v-model="payChannelForm.bean_service_name"></el-input>
                </el-form-item>
                <el-form-item label="" prop="enabled">
                    <el-input v-model="payChannelForm.enabled"></el-input>
                </el-form-item>
                <el-form-item label="" prop="sorted">
                    <el-input v-model="payChannelForm.sorted"></el-input>
                </el-form-item>
                <el-form-item label="" prop="create_time">
                    <el-input v-model="payChannelForm.create_time"></el-input>
                </el-form-item>
                <el-form-item label="" prop="code">
                    <el-input v-model="payChannelForm.code"></el-input>
                </el-form-item>
                <el-form-item label="" prop="description">
                    <el-input v-model="payChannelForm.description"></el-input>
                </el-form-item>
                <el-form-item label="" prop="pre_code">
                    <el-input v-model="payChannelForm.pre_code"></el-input>
                </el-form-item>
                <el-form-item label="" prop="code_expire_time">
                    <el-input v-model="payChannelForm.code_expire_time"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :loading="btnLoading" @click="submit">{{textMap[status].label}}
                    </el-button>
                    <el-button @click="close">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
  import { addObj, putObj } from '@/api/trade/pay-channel'

  const textMap = {
    update: {
      title: '更新交易第三方渠道表',
      label: '更新'
    },
    create: {
      title: '新建交易第三方渠道表',
      label: '保存'
    }
  }
  const payChannelForm = {
    id: undefined,
    provider_id: undefined,
    name: undefined,
    icon: undefined,
    bean_service_name: undefined,
    enabled: undefined,
    sorted: undefined,
    create_time: undefined,
    code: undefined,
    description: undefined,
    pre_code: undefined,
    code_expire_time: undefined
  }
  export default {
    data () {
      return {
        dialogVisible: false,
        textMap: Object.assign({}, textMap),
        payChannelForm: Object.assign({}, payChannelForm),
        status: 'create',
        btnLoading: false,
        rules: {
          id: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          provider_id: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '图标名称不能为空', trigger: 'blur' }
          ],
          bean_service_name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          enabled: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sorted: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          create_time: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          pre_code: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          code_expire_time: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      submit () {
        if (this.status === 'update') {
          this.update()
        }
        if (this.status === 'create') {
          this.save()
        }
      },
      update () {
        this.$refs['payChannelForm'].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            putObj(this.payChannelForm).then(res => {
              this.$message({
                message: '更新成功',
                type: 'success'
              })
              this.btnLoading = false
              this.close()
            }).catch(err => {
              this.$message({
                message: '更新失败【' + err.message + '】',
                type: 'error'
              })
              this.btnLoading = false
            })
          } else {
            return false
          }
        })
      },
      save () {
        this.$refs['payChannelForm'].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            addObj(this.payChannelForm).then(res => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.btnLoading = false
              this.close()
            }).catch(err => {
              this.$message({
                message: '新增失败【' + err.message + '】',
                type: 'error'
              })
              this.btnLoading = false
            })
          } else {
            return false
          }
        })
      },
      resetForm (formName) {
        this.$refs['payChannelForm'].resetFields()
      },
      open: function (status, formData) {
        this.status = status
        this.dialogVisible = true
        if (this.payChannelForm) {
          this.payChannelForm = formData
        }
      },
      close: function () {
        this.dialogVisible = false
      }
    }
  }
</script>

<style>

</style>
